// 非代理模式

/*var Flower = function() {};

var xiaoming = {

	sendFlower: function(target) {

		var flower = new Flower();

		target.receiveFlower(flower);

	}

}



var A = {

	receiveFlower: function(flower) {

		console.log('收到花', flower);

	}

}



xiaoming.sendFlower(A);*/


// 代理模式一  通过中介将花送给第三个人

/*var Flower = function() {}

var xiaoming = {

	sendFlower: function(target) {

		var flower = new Flower();

		target.receiveFlower(flower);

	}

}



var B = {

	receiveFlower: function(flower) {

		A.receiveFlower(flower)

	}

}



var A = {

	receiveFlower: function(flower) {

		console.log('收到花', flower);

	}

}



xiaoming.sendFlower(B);*/


// 通过中间人在A心情好时将花送给A

/*var Flower = function() {}

var xiaoming = {

	sendFlower: function(target) {

		var flower = new Flower();

		target.receiveFlower(flower);

	}

}



var B = {

	receiveFlower: function(flower) {

		A.listenGoodMood(function() {

			A.receiveFlower(flower)

		})

	}

}



var A = {

	receiveFlower: function(flower) {

		console.log('收到花', flower);

	},



	listenGoodMood: function(fn) {

		setTimeout(function() {

			console.log('good mood');

			fn();

		}, 2000)

	}

}



xiaoming.sendFlower(B);*/


// 图片懒加载的代理模式

/*var myImage = (function(){

	var imageNode = document.createElement('img');

	document.querySelector('.container').appendChild(imageNode);

	imageNode.width = 600;

	imageNode.height = 400;

	return {

		setSrc: function(src) {

			imageNode.src = src;

		}

	}

})();



var proxyImage = (function() {

	var img = new Image();

	img.onload = function() {

		myImage.setSrc(this.src)

	}



	return {

		setSrc: function(src) {

			myImage.setSrc('../product-img-4.png')

			img.src = src;

		}

	}

})()



proxyImage.setSrc('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607487658703&di=b665fd2122e7339aa230acea2241f840&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F06%2F20161006190647_M3akn.jpeg');*/


// 不使用代理模式

/*var MyImage = (function() {

	var image = document.createElement('img');

	var img = new Image();

	image.width = 600;

	image.height = 400;

	document.querySelector('.container').appendChild(image);



	img.onload = function() {

		image.src = this.src;

	}



	return {

		setSrc: function(src) {

			image.src = '../u271.png';

			img.src = src;

		}

	}

})();

MyImage.setSrc('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607487658703&di=b665fd2122e7339aa230acea2241f840&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F06%2F20161006190647_M3akn.jpeg');*/


// 代理与本地的接口一致

/*var myImage = (function() {

	var image = document.createElement('img');

	image.style.width = '100%';

	document.querySelector('.container').appendChild(image);

	return function(src) {

		image.src = src;

	}

})();



var proxyImage = (function() {

	var img = new Image;



	img.onload = function() {

		myImage(this.src)

	}



	return function(src) {

		myImage('../u271.png');

		img.src = src;

	}

})();



proxyImage('https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1607487658703&di=b665fd2122e7339aa230acea2241f840&imgtype=0&src=http%3A%2F%2Fc-ssl.duitang.com%2Fuploads%2Fitem%2F201610%2F06%2F20161006190647_M3akn.jpeg');*/


// 模拟数据同步

/*var synchronousFile = function(ids) {

	console.log('开始同步文件,id有: ' + ids)

}



var checkBoxs = document.querySelectorAll('input');



for (var i = 0, c; c = checkBoxs[i++];) {

	c.onclick = function() {

		if (this.checked === true) {

			synchronousFile(this.id);

			console.log(i);

		}

	}

}*/


// 使用代理模式 在同步数据之前 收集两秒内的同步数据  一次发送

/*var synchronousFile = function(ids) {

	console.log('同步数据id有：' + ids);

}



var proxySyncronousFile = (function() {

	var cache = [],

		timer = null;



	return function(id) {

		cache.push(id);



		if (timer) {

			return;

		}



		timer = setTimeout(function() {

			// 输出的是两秒内选中的所有input

			synchronousFile(cache.join(','));

			cache = [];

			clearTimeout(timer);

			timer = null;

		}, 2000)

	}

})();



var checkBoxs = document.querySelectorAll('input');



for (var i = 0, c; c = checkBoxs[i++];) {

	c.onclick = function() {

		if (this.checked === true) {

			proxySyncronousFile(this.id);

		}

	}

}*/


// 简单miniConsole.js

// 1.miniConsole.js文件比较大，在一开始也不需要去加载，并不是每个用户都会使用

// 2.当用户在按下F2调出miniConsole控制台，再加载miniConsole.js

// 3.在用户没有按下F2之前，使用miniConsole代理对象中完成，将函数放在队列中

/*var cache = [];
var miniConsole = {
	log: function() {
		var args = arguments;
		cache.push(function() {
			return miniConsole.log.apply(miniConsole, args);
		})
	}
}



var handler = function(ev) {
	if (ev.keyCode === 113) {
		var script = document.createElement('script');
		script.onload = function() {
			for (var i = 0, fn; fn = cache[i++];) {
				fn()
			}
		}
		script.src = 'miniConsole.js';
		document.getElementsByTagName('head')[0].appendChild(script);
	}
}
document.body.addEventListener('keydown', handler, false);



// 优化
var miniConsole = (function() {
	var cache = [];
	var handler = function(ev) {
		if (ev.keyCode === 113) {
			var script = document.createElement('script');
			script.onload = function() {
				for (var i = 0, fn; fn = cache[i++];) {
					fn()
				}
			}
			script.src = 'miniConsole.js';
			document.querySelector('head').appendChild(script);
			document.body.removeEventListener('keydown', handler);
		}
	}
	document.body.addEventListener('keydown', handler, false);

	return {
		log: function() {
			var args = arguments;
			cache.push(function() {
				return miniConsole.log.apply(miniConsole, args);
				// return miniConsole.log(...args);
			})
		}
	}
})();



miniConsole.log('1')
miniConsole.log('2')
miniConsole.log('3')
miniConsole.log('4')
miniConsole.log('5')*/

// 乘积的缓存代理
var mult = function () {
    var a = 1;
    for (var i = 0, l = arguments.length; i < l; i++) {
        a = a * arguments[i]
    }
    return a;
}


// console.log(mult(1,2,3,4,5,6));
var proxyMult = (function () {
    var cache = {};
    return function () {
        var args = Array.prototype.join.call(arguments);
        if (args in cache) {
            return cache[args]
        }
        return cache[args] = mult.apply(null, arguments);
    }
})();

console.time('init');
console.log(proxyMult(5, 6, 7, 8, 9, 10, 11));
console.timeEnd('init');
setTimeout(function () {
    console.time('cache');
    console.log(proxyMult(5, 6, 7, 8, 9, 10, 11));
    console.timeEnd('cache');
}, 3000);